For this leaflet assigment I have choosen to create a map of candian earthquakes based on the data provided here: http://geoscan.nrcan.gc.ca/starweb/geoscan/servlet.starweb?path=geoscan/downloade.web&search1=R=296908
library(dplyr)
library(leaflet)
library(RColorBrewer)
First let’s import the data. I only care about the first 4 columns for my map so I’m going to drop the rest.
data_dir <- "./data"
data_file <- paste(data_dir, "/SHEEF2010.csv", sep = "")
earthquakes <- read.csv(data_file, stringsAsFactors = FALSE)[,1:4]
str(earthquakes)
## 'data.frame': 41690 obs. of 4 variables:
## $ Date : num 1.63e+11 1.64e+11 1.64e+11 1.64e+11 1.64e+11 ...
## $ MW : num 3.4 6.5 2.4 2.4 2.9 2.9 2.4 3.1 5.3 7 ...
## $ Longitude: num -70.8 -71.8 -70.9 -71 -70.8 ...
## $ Latitude : num 42.6 44.4 42.5 42.5 42.8 42.8 42.6 42.5 45.5 47.6 ...
Let’s change the date to date format and create a column for labels
## Convert to date
earthquakes$Date <- strptime(earthquakes$Date,"%Y%m%d%H%M", tz='GMT')
## Create labels with magnitude and date
labels <- paste("Mag", earthquakes$MW, "-", as.character(earthquakes$Date))
earthquakes <- cbind(earthquakes, labels)
Now let’s select a palette and build the map
##Create palette
pal <- colorNumeric("YlOrRd", domain = earthquakes$MW)
##Generate map with markers and legends
test_map <- leaflet(data = earthquakes) %>%
addTiles() %>%
addCircleMarkers(fillOpacity = .6, fillColor = ~pal(earthquakes$MW), weight = 0, radius = (earthquakes$MW^2)/2.3, label = labels) %>%
addLegend(pal=pal, values=earthquakes$MW, opacity = 1, title = "Magnitude")
test_map
Canadian Earthquakes